﻿Imports System.Data.OleDb

Public Class Profile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString())

        Dim ID_string As String = " SELECT [UserID] FROM aspnet_Users WHERE UserName = '" & User.Identity.Name & "' "
        Dim findIDCmd As New OleDbCommand(ID_string, Conn)
        Conn.Open()
        Dim ID As OleDbDataReader = findIDCmd.ExecuteReader
        ID.Read()
        Dim UserID As Integer = ID("UserID")
        Session("UserID") = UserID


        Dim Details_string As String = "SELECT UD.FirstName, UD.LastName, MT.MembershipType, UD.Country, UD.Occupation,  UD.AboutUser, UD.Occ_Desc, UD.Image FROM UserDetails AS UD INNER JOIN MembersType AS MT ON UD.MembershipID_FK=MT.MembershipID WHERE ((UD.[USERID_FK] = " & UserID & " )); "
        Dim Details_command As OleDb.OleDbCommand = New OleDb.OleDbCommand(Details_string, Conn)
        Dim Det_reader As OleDb.OleDbDataReader = Details_command.ExecuteReader()
        If Det_reader.HasRows Then
            While Det_reader.Read()
                lbl_FirstName.Text = Det_reader.Item(0).ToString()
                lbl_SecondName.Text = Det_reader.Item(1).ToString()
                lbl_Level.Text = Det_reader.Item(2).ToString()
                lbl_Country.Text = Det_reader.Item(3).ToString()
                lbl_Occupation.Text = Det_reader.Item(4).ToString()
                lbl_AboutUser.Text = Det_reader.Item(5).ToString()
                lbl_occ_dec.Text = Det_reader.Item(6).ToString()
                Image1.ImageUrl = Det_reader.Item(7).ToString()
            End While
        Else
            Image1.ImageUrl = "/Images/Users/DefaultImage.png"
        End If
        lbl_UserName.Text = User.Identity.Name

        Dim User_string As String = "SELECT CREATEDATE, LASTLOGINDATE FROM ASPNET_MEMBERSHIP WHERE USERID=" & UserID
        Dim Usr_command As OleDb.OleDbCommand = New OleDb.OleDbCommand(User_string, Conn)
        Dim Usr_reader As OleDb.OleDbDataReader = Usr_command.ExecuteReader()
        If Usr_reader.HasRows Then
            While Usr_reader.Read()
                lbl_Dateofjoin.Text = Usr_reader.Item(0).ToString()
                lbl_LastLogin.Text = Usr_reader.Item(1).ToString()
            End While

        End If

        Dim post_string As String = "SELECT TOP 1 ID FROM USERACTIVITY WHERE USERID_FK=" & UserID
        Dim post_command As OleDb.OleDbCommand = New OleDb.OleDbCommand(post_string, Conn)
        Dim post_reader As OleDb.OleDbDataReader = post_command.ExecuteReader()
        If post_reader.HasRows Then

            Dim pos_string As String = "SELECT COUNT(ID), SUM(POINTS) FROM USERACTIVITY WHERE USERID_FK=" & UserID
            Dim pos_command As OleDb.OleDbCommand = New OleDb.OleDbCommand(pos_string, Conn)
            Dim pos_reader As OleDb.OleDbDataReader = pos_command.ExecuteReader()
            While pos_reader.Read()
                lbl_Posts.Text = pos_reader.Item(0).ToString()
                Dim point As Double = pos_reader.Item(1).ToString()
                Dim nextpoint As Double = 500 - point
                lbl_Points.Text = point
                lbl_PointsToNextLevel.Text = nextpoint

            End While
        Else
            lbl_Posts.Text = "0"
            lbl_Points.Text = "0"
            lbl_PointThisWeek.Text = "0"
            lbl_PointsToNextLevel.Text = "500"

        End If

    End Sub

End Class